package com.example.demo.itemWriter.ItemWriterToDataBase;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableBatchProcessing
public class ItemWriterDatabaseDemo {
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    @Qualifier("flatFileReader")
    private ItemReader<Customer> flatFileReader;
    @Autowired
    @Qualifier("itemWriterToDatabase")
    private ItemWriter<Customer> itemWriterToDatabase;

    @Bean
    public Job   itemWriterDatabaseDemoJob(){
        return jobBuilderFactory.get("itemWriterDatabaseDemoJob")
                .start(itemWriterDatabaseDemoStep())
                .build();
    }

    @Bean
    public Step itemWriterDatabaseDemoStep() {
        return stepBuilderFactory.get("itemWriterDatabaseDemoStep")
                .<Customer,Customer>chunk(100)
                .reader(flatFileReader)
                .writer(itemWriterToDatabase)
                .build();


    }

}
